<?php $this->beginBlock('cssHeaderFile')?>
<link rel="stylesheet" href="/css/ucenter.css">
<?php $this->endBlock()?>

<?php $this->beginBlock('isLogin')?>
    <!--页面右侧-->
    <div class="navbar-header pull-right manual-menu">
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                    <?=!Yii::$app->user->isGuest ? Yii::$app->user->identity->username :'个人中心'?> <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
<!--                    <li><a href="javascript:" data-toggle="modal" data-target="#shareProject">项目分享</a> </li>-->
<!--                    <li><a href="/export/ceshisss?output=pdf" target="_blank">项目导出PDF</a> </li>-->
                    <li><a href="#" title="基本资料">基本资料</a> </li>
                    <li><a href="<?=\yii\helpers\Url::toRoute(['ucenter/index'])?>" title="我的项目">我的项目</a> </li>
                    <li><a href="/" title="返回首页">返回首页</a> </li>
                    <?php if (!Yii::$app->user->isGuest):?>
                    <li class="divider"></li>
                    <li><a href="<?=\yii\helpers\Url::toRoute(['ucenter/logout'])?>"> 退出登录</a></li>
                    <?php endif;?>
                </ul>
            </li>
        </ul>
    </div>
<?php $this->endBlock()?>

<?php $this->beginBlock('pageTitle')?>
<a href="/" title="FankersDoc" class="book-title">
    FankersDoc
</a>
<?php $this->endBlock()?>


<div class="container" style="margin-top: 70px">
    <div class="row" id="projectList">
        <div class="text-center col-sm-3" v-if="orderProject.length>0" v-for="(item,index) in orderProject">
            <span class="my-item-setting" title="编辑" style="display: block;">
                <i class="glyphicon glyphicon-edit"></i>
            </span>
            <span class="my-item-top" title="授权" style="display: block;">
                <i class="glyphicon glyphicon-globe"></i>
            </span>

            <a class="thumbnail my-item" :href="'/docs/index?projectId='+item.id">
              <p v-text="item.name"></p>
            </a>
        </div>
        <div class="text-center col-sm-3" data-toggle="modal" data-target="#addProject">
            <a class="thumbnail my-item" href="javascript:;" title="添加一个新项目添加一个新项目">
                <p>新建项目&nbsp;<i class="fa fa-plus"></i></p>
            </a>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="addProject" tabindex="-1" role="dialog" aria-labelledby="addProjectLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="addProjectLabel">添加项目</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <form id="myProjectForm" onsubmit="return false;">
                        <input name="<?= Yii::$app->request->csrfParam;?>" type="hidden" value="<?= Yii::$app->request->csrfToken ?>">
                        <input name="id" type="hidden">
                        <div class="col-sm-12">
                            <div class="input-group">
                                <span class="input-group-addon">序号</span>
                                <input type="text" name="sort" class="form-control" placeholder="序号：0/1/2等">
                            </div>
                        </div>

                        <div class="col-sm-12 m-t-20">
                            <div class="input-group">
                                <span class="input-group-addon">名称</span>
                                <input type="text" name="name" class="form-control" placeholder="项目名称">
                            </div>
                        </div>
                        <div class="col-sm-12 m-t-20">
                            <div class="input-group">
                                <span class="input-group-addon">描述</span>
                                <textarea class="form-control" name="desc" placeholder="项目描述"></textarea>
                            </div>
                        </div>
                        <div class="col-sm-12 m-t-20">
                            <div class="input-group">
                                <span class="input-group-addon">公开</span>
                                <select class="form-control" name="is_public">
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="saveData.createProject()" class="btn btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock('jsFooterText')?>
<script>
    var vue = new Vue({
        el: '#projectList',
        data: {
            projects:<?= !isset($projects['items']) || empty($projects['items']) ? '{}' : json_encode($projects['items'],true)?>
        },
        computed:{
            orderProject:function () {
                if(JSON.stringify(this.projects) === "{}")return this.projects;
                returnData = this.projects.sort(function (a,b) {
                    return b.sort-a.sort;
                });

                return returnData;
            }
        }
    });
    var saveData = {
        ajaxLock:false,
        createProject:function () {
            that = this;
            var data = $("#myProjectForm").serialize();
            if (that.ajaxLock)return false;
            that.ajaxLock = true;
            var url = "<?=\yii\helpers\Url::toRoute(['project/save'])?>";
            $.ajax({
                type:'post',
                data:data,
                dataType:'json',
                url:url,
                success:function (resp) {
                    that.ajaxLock = false;
                    if (resp.e === '0'){
                        if(JSON.stringify(this.vue) === "{}")vue.projects = [];
                        vue.projects.push(resp.d);
                        layer.msg(resp.m, {icon: 6});
                    } else {
                        layer.msg(resp.m, {icon: 5});
                    }

                    $('#addProject').modal('hide');
                },
                error:function () {
                    that.ajaxLock = false;
                    layer.msg('服务器正忙，请稍后重试', {icon: 5});
                }
            });
        }
    };
</script>
<?php $this->endBlock()?>
